package StreamAPI增强;

import java.util.List;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {

        //takewhile：从Stream中依次获取满足条件的元素，直到不满足条件为止
        //一旦条件不满足的时候，后面的所有元素都会被忽略
        List<Integer> list = Stream.of(2, 2, 3, 4, 5, 6, 7, 8, 9, 10)
                .takeWhile(x -> x % 2 == 0)
                .toList();
        //2,2
//        list.forEach(System.out::println);

        //dropwhile：顺序去掉符合条件的值，直到不满条件时终止判断
        List<Integer> dropList = Stream.of(2, 2, 3, 4, 5, 6, 7, 8, 9, 10)
                .dropWhile(x -> x % 2 == 0)
                .toList();
        //3,4,5,6,7,8,9,10
//        dropList.forEach(System.out::println);

        //ofNullable 支持传入空流，若没有这个且传入一个空流，会抛出NPE
        var count = Stream.ofNullable(null).count();
        System.out.println(count);//0

        //迭代器
        //输出0-9
//        Stream.iterate(0,n->n<10,n->n+1).forEach(x-> System.out.print(x+" "));
//        System.out.println();
//        Stream.iterate(0,n->n+1).limit(10).forEach(x->System.out.print(x+" "));

    }
}
